﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DOTNET101_Exceptions
{
    class Program
    {
        static void Main(string[] args)
        {
        }

        class CustomException : Exception
        {
            public CustomException(string message)
            {

            }

        }
        private static void TestThrow()
        {
            CustomException ex =
                new CustomException("Custom exception in TestThrow()");

            throw ex;
        }

        static void TestCatch()
        {
            try
            {
                TestThrow();
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(ex.ToString());
            }
        }

        static void TestFinally()
        {
            System.IO.FileStream file = null;
            //Change the path to something that works on your machine
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(@"C:\file.txt");

            try
            {
                file = fileInfo.OpenWrite();
                file.WriteByte(0xF);
            }
            finally
            {
                // Closing the file allows you to reopen it immediately - otherwise IOException is thrown.
                if (file != null)
                {
                    file.Close();
                }
            }

            try
            {
                file = fileInfo.OpenWrite();
                System.Console.WriteLine("OpenWrite() succeeded");
            }
            catch (System.IO.IOException)
            {
                System.Console.WriteLine("OpenWrite() failed");
            }
        }


        static void CodeWithCleanup()
        {
            System.IO.FileStream file = null;
            System.IO.FileInfo fileInfo = null;

            try
            {
                fileInfo = new System.IO.FileInfo("C:\\file.txt");

                file = fileInfo.OpenWrite();
                file.WriteByte(0xF);
            }
            catch (System.UnauthorizedAccessException e)
            {
                System.Console.WriteLine(e.Message);
            }
            finally
            {
                if (file != null)
                {
                    file.Close();
                }
            }
        }

        class TestTryCatch
        {
            static int GetInt(int[] array, int index)
            {
                try
                {
                    return array[index];
                }
                catch (System.IndexOutOfRangeException e)  // CS0168
                {
                    System.Console.WriteLine(e.Message);
                    //set IndexOutOfRangeException to the new exception's InnerException
                    throw new System.ArgumentOutOfRangeException("index parameter is out of range.", e);
                }
            }
        }
    }
}
